package com.nt.mes_leader.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/10/29.
 */
public class DateUtilsl
{
    //获取系统时间 格式为："yyyy/MM/dd "*/
    public static String getCurrentDate()
    {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }
    //*时间戳转换成字符窜*/
    public static String getDateToString(long time)
    {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }


    /**
     * 将字符串转为long时间戳
     * @param time 字符串
     * @return  时间戳
     */
    public static long getStringToDate(String time)
    {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try
        {
            date = sf.parse(time);
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    //字符串转时间戳

    /**
     * 字符串转String时间戳
     * @param timeString
     * @return
     */
    public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp.substring(0,10);
    }

    public static String date2TimeStamp(String dateStr){
        return date2TimeStamp(dateStr, "yyyy年MM月dd日 HH时mm分ss秒");
    }

    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //时间戳转字符串

    /**
     * 时间戳转String字符串
     * @param timeStamp
     * @return
     */
    public static String getStrTime2(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        long  l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }




    /**
     * String时间戳 转 时间String字符串 yyyy-MM-dd HH:mm:ss
     * @param seconds
     * @return
     */
    public static String ConvertToDatetime(String seconds)
    {
         if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
               return "";
         }
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    /**
     * String时间戳 转 时间String字符串 yyyy-MM-dd
     * @param seconds
     * @return
     */
    public static String ConvertToDate(String seconds)
    {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }


    /**
     * 获取系统当前时间
     * @return
     */
    public static String getDataTime()
    {
        // TODO Auto-generated method stub
        int day = 0;
        int year = 0;
        int month = 0;// 系统日期从0开始算起
        int hour = 0;// 小时
        int minute = 0;// 分
        int second = 0;// 秒s
        Calendar c = Calendar.getInstance();// 获得系统当前日期
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;// 系统日期从0开始算起
        day = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);// 小时
        minute = c.get(Calendar.MINUTE);// 分
        second = c.get(Calendar.SECOND);// 秒
        String m = "";
        if(month < 10)
        {
            m = "0"+month;
        }
        else
        {
            m = String.valueOf(month);
        }
        String date = year + "/" + m + "/" + day + " " + hour + ":" + minute + ":" + second;
        return date;
    }

    public static String getYesterdayDataTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(cal.getTime());
        System.out.println(yesterday);
        return yesterday;
    }

    public static String getTimeStamp(String time)
    {
        return DateUtilsl.getTime(time);
    }

    /**
     * start
     * 本周开始时间戳 - 以星期一为本周的第一天
     */
    public static String getWeekStartTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        cal.add(Calendar.DATE , -day_of_week + 1 );
        //return simpleDateFormat.format(cal.getTime()) + "000000000";
        return getTimeStamp(simpleDateFormat.format(cal.getTime()));
    }

    /**
     * end
     * 本周结束时间戳 - 以星期一为本周的第一天
     */
    public static String getWeekEndTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        cal.add(Calendar.DATE , - day_of_week + 7 );
        //return simpleDateFormat.format(cal.getTime()) + "235959999";
        return getTimeStamp(simpleDateFormat.format(cal.getTime()));
    }

    /**
     * start
     * 本周开始时间戳 - 以星期一为本周的第一天
     */
    public static String getLastWeekStartTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        cal.add(Calendar.DATE , - day_of_week - 6  );
        //return simpleDateFormat.format(cal.getTime()) + "000000000";
        return getTimeStamp(simpleDateFormat.format(cal.getTime()));
    }

    /**
     * end
     * 本周结束时间戳 - 以星期一为本周的第一天
     */
    public static String getLastWeekEndTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        cal.add(Calendar.DATE , - day_of_week  );
        //return simpleDateFormat.format(cal.getTime()) + "235959999";
        return getTimeStamp(simpleDateFormat.format(cal.getTime()));
    }






    /**得到当年当月的最大日期**/
    public static int MaxDayFromDay_OF_MONTH(int year,int month){
        Calendar time= Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR,year);
        time.set(Calendar.MONTH,month-1);//注意,Calendar对象默认一月为0
        int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }




}
